import hilog from '@ohos.hilog';
// @ts-ignore
import Ability from '@ohos.application.Ability'
import Window from '@ohos.window'
import MyUtils from '../Common/Utils'
import abilityAccessCtrl from '@ohos.abilityAccessCtrl';

export default class MainAbility extends Ability {
    private utils: MyUtils = new MyUtils();

    onCreate(want, launchParam) {
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
        hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
        hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
        // @ts-ignore
        globalThis.abilityContext = this.context;
        // @ts-ignore
        globalThis.myContext = this.context;
        try {
            this.getPermission();
        } catch (err) {
            this.utils.myErrLog("err is " + err);
        }
    }

    onDestroy() {
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
    }

    onWindowStageCreate(windowStage: Window.WindowStage) {
        // Main window is created, set main page for this ability
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

        windowStage.loadContent('pages/index', (err, data) => {
            if (err.code) {
                hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR);
                hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
                return;
            }
            hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
            hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
        });
    }

    onWindowStageDestroy() {
        // Main window is destroyed, release UI related resources
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
    }

    onForeground() {
        // Ability has brought to foreground
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
    }

    onBackground() {
        // Ability has back to background
        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
    }

    getPermission() {
        let array: Array<string> = [
            "ohos.permission.READ_MEDIA",
            "ohos.permission.WRITE_MEDIA",
            "ohos.permission.MEDIA_LOCATION",
            "ohos.permission.GET_BUNDLE_INFO",
            "ohos.permission.DISTRIBUTED_DATASYNC",
            "ohos.permission.START_ABILITIES_FROM_BACKGROUND",
            "ohos.permission.START_INVISIBLE_ABILITY",
            "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",
            "ohos.permission.GET_DISTRIBUTED_ACCOUNTS",
            "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
            "ohos.permission.ACCESS_SERVICE_DM"
        ];

        let atManager = abilityAccessCtrl.createAtManager();
        //requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
        // @ts-ignore
        atManager.requestPermissionsFromUser(this.context, array).then(function (data) {

            this.utils.myLog("data type:" + typeof (data));
            this.utils.myLog("data:" + data);
            this.utils.myLog("data permissions:" + data.permissions);
            this.utils.myLog("data result:" + data.authResults);
        }, (err) => {
            console.error('Failed to start ability', err.code);
            this.utils.myLog('Failed to start ability: ' + err.code);
        });
    }
}
